package com.ruoyi.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.UserComplaints;
import com.ruoyi.user.service.IUserComplaintsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;

/**
 * 用户投诉Controller
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/user/complaints")
public class UserComplaintsController extends BaseController
{
    @Autowired
    private IUserComplaintsService userComplaintsService;

    /**
     * 查询用户投诉列表
     */
    @PreAuthorize("@ss.hasPermi('user:complaints:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserComplaints userComplaints)
    {
        startPage();
        List<UserComplaints> list = userComplaintsService.selectUserComplaintsList(userComplaints);
        return getDataTable(list);
    }



    /**
     * 导出用户投诉列表
     */
    @PreAuthorize("@ss.hasPermi('user:complaints:export')")
    @Log(title = "用户投诉", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserComplaints userComplaints)
    {
        List<UserComplaints> list = userComplaintsService.selectUserComplaintsList(userComplaints);
        ExcelUtil<UserComplaints> util = new ExcelUtil<UserComplaints>(UserComplaints.class);
        util.exportExcel(response, list, "用户投诉数据");
    }

    /**
     * 获取用户投诉详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:complaints:query')")
    @GetMapping(value = "/{complaintId}")
    public AjaxResult getInfo(@PathVariable("complaintId") String complaintId)
    {
        return success(userComplaintsService.selectUserComplaintsByComplaintId(complaintId));
    }



    /**
     * 新增用户投诉
     */
    @PreAuthorize("@ss.hasPermi('user:complaints:add')")
    @Log(title = "用户投诉", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserComplaints userComplaints)
    {
        return toAjax(userComplaintsService.insertUserComplaints(userComplaints));
    }



    /**
     * 修改用户投诉
     */
    @PreAuthorize("@ss.hasPermi('user:complaints:edit')")
    @Log(title = "用户投诉", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserComplaints userComplaints)
    {
        return toAjax(userComplaintsService.updateUserComplaints(userComplaints));
    }

    /**
     * 删除用户投诉
     */
    @PreAuthorize("@ss.hasPermi('user:complaints:remove')")
    @Log(title = "用户投诉", businessType = BusinessType.DELETE)
	@DeleteMapping("/{complaintIds}")
    public AjaxResult remove(@PathVariable String[] complaintIds)
    {
        return toAjax(userComplaintsService.deleteUserComplaintsByComplaintIds(complaintIds));
    }

    /**
     * 查询“我的投诉”列表（当前登录用户）
     */
    @PreAuthorize("@ss.hasPermi('user:mycomplaint:list')")
    @GetMapping("/myComplaints/{customerId}")
    public TableDataInfo listMy(@PathVariable("customerId") String customerId)
    {
        startPage();
        List<UserComplaints> list = userComplaintsService.selectUserComplaintsListByCustomerId(customerId);
        return getDataTable(list);
    }

    /**
     * 获取当前用户的投诉详情（归属校验）
     */
    @PreAuthorize("@ss.hasPermi('user:mycomplaint:details:query')")
    @GetMapping(value = "/{customerId}/complaint/detail/{complaintId}")
    public AjaxResult getMyComplaint( @PathVariable("customerId") String customerId,@PathVariable("complaintId") String complaintId)
    {
        UserComplaints detail = userComplaintsService.selectUserComplaintByIdAndCustomer(complaintId, customerId);
        if (detail == null)
        {
            return AjaxResult.error("记录不存在或无权查看");
        }
        return success(detail);
    }

    /**
     * 当前用户提交投诉（自动绑定用户ID）
     */
    @PreAuthorize("@ss.hasPermi('user:mycomplaint:add')")
    @PostMapping("/{customerId}/submit")
    public AjaxResult submit(@PathVariable("customerId") String customerId ,@RequestBody UserComplaints userComplaints)
    {
        userComplaints.setCustomerId(customerId);
        if (userComplaints.getStatus() == null)
        {
            userComplaints.setStatus(0L);
        }
        userComplaints.setCreatedAt(DateUtils.getNowDate());
        userComplaints.setUpdatedAt(DateUtils.getNowDate());
        int rows = userComplaintsService.insertUserComplaints(userComplaints);
        return rows > 0 ? success(userComplaints) : AjaxResult.error("提交失败");
    }

    /**
     * 当前用户撤销投诉（软删除）
     */
    @PreAuthorize("@ss.hasPermi('user:mycomplaint:remove')")
    @PostMapping("/{customerId}/cancel/{com" +
            "plaintId}")
    public AjaxResult cancel( @PathVariable("customerId")String customerId,@PathVariable("complaintId") String complaintId)
    {

        int rows = userComplaintsService.softDeleteUserComplaintByIdAndCustomer(
                complaintId, customerId, DateUtils.getNowDate(), DateUtils.getNowDate());
        return rows > 0 ? success() : AjaxResult.error("撤销失败或无权操作");
    }
}
